﻿using System;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class RollMessage : MonoBehaviour
{
    public struct MessagInfo
    {
        public int priority;
        public string msg;
        public float rollTime;

        public MessagInfo(int p, string m, float t)
        {
            priority = p;
            msg = m;
            rollTime = t;

        }
    }
    public List<MessagInfo> messageList;
    public bool startRolling;
    public float waitTime;

    private RectTransform parentRect;
    private RectTransform selfRect;
    private ContentSizeFitter sizeFitter;
    private Text msgText;
    private float timer;
    private bool startWait;

    void Start()
    {
        selfRect = this.transform.GetComponent<RectTransform>();
        msgText = this.transform.GetComponent<Text>();
        sizeFitter = this.transform.GetComponent<ContentSizeFitter>();
        parentRect = this.transform.parent.GetComponent<RectTransform>();
        messageList = new List<MessagInfo>();
        selfRect.anchoredPosition = Vector2.zero;

        AddMessageToList(1, "各位肮海士们大家好，目前距离服务嚣关闭还有30分钟", 10);
        AddMessageToList(1, "春节活动开始啦，初一到初十期间，充值双倍返利活动返场了", 10);
        AddMessageToList(1, "距离公告联赛开始还有30分钟", 10);
        AddMessageToList(1, "各位航海士们大家好，XXX玩法出现的BUG已收到反馈，正在处理中", 10);

        parentRect.anchoredPosition = Vector2.zero;
        parentRect.transform.localScale = Vector3.zero;
        DOTween.To(() => parentRect.anchoredPosition, (f) => parentRect.anchoredPosition = f,
            new Vector2(0, -20),2);
        Tweener t = parentRect.DOScale(Vector3.one, 2);
        t.onComplete = () => { startRolling = true; };
    }

    private void AddMessageToList(int priority,string msg,float rolltime)
    {
        MessagInfo msgInfo = new MessagInfo(priority,msg, rolltime);
        messageList.Add(msgInfo);
    }

    void Update()
    {
        if (startRolling && messageList.Count > 0)
        {
            RollMsg();
        }

        if (startWait)
        {
            timer += Time.deltaTime;
            if (timer > waitTime)
            {
                timer = 0;
                startWait = false;
                messageList.RemoveAt(0);
                if (messageList.Count > 0)
                {
                    startRolling = true;
                }
            }
        }
    }

    private void RollMsg()
    {
        startRolling = false;
        MessagInfo msg = messageList[0];
        msgText.text = msg.msg;
        float endValue = -(parentRect.sizeDelta.x + selfRect.sizeDelta.x + 50);
        Tween t = DOTween.To(() => selfRect.anchoredPosition, (f) => selfRect.anchoredPosition = f,
            new Vector2(endValue,0), msg.rollTime);

        t.onComplete = () =>
        {
            if (messageList.Count > 1)
            {
                selfRect.anchoredPosition = Vector2.zero;
                msgText.text = messageList[1].msg;
            }
            startWait = true;
        };
    }
}
